package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    public void add(ShoppingCartDTO shoppingCartDTO) {
        //ShoppingCart(semealId,dishId,userId)
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //查询购物车是否已经存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果存在则数量加1
        if(list!=null&&list.size() > 0){
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartMapper.updateNumber(shoppingCart1);
        }else{//如果不存在则添加
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
             if(dishId != null){
                 Dish dish = dishMapper.queryById(dishId);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
            }else {//setmealId != null
                 Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.addShoppingCart(shoppingCart);
        }

    }

    public List<ShoppingCart> list() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart build = ShoppingCart.builder().userId(currentId).build();
        List<ShoppingCart> list = shoppingCartMapper.list(build);

        return list;
    }

    public void delete(ShoppingCartDTO shoppingCartDTO) {
        //创建ShoppingCart(semealId,dishId,userId)
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(currentId).build();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //查询购物车是否已经存在
        List list = shoppingCartMapper.list(shoppingCart);
        if(list != null && !list.isEmpty()) {
            ShoppingCart shoppingCart1 = (ShoppingCart) list.get(0);
            //如果数量大于1则数量减1
            if (shoppingCart1.getNumber() > 1) {
                shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
                shoppingCartMapper.updateNumber(shoppingCart1);
                return;
            } else {//如果数量等于1则删除
                shoppingCartMapper.delete(shoppingCart);
            }
        }
    }

    public void clear() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.clear(currentId);

    }


}
